//
//  MAAdvTipView.m
//  MateAdv
//
//  Created by 茶 on 2023/9/12.
//

#import "MAAdvTipView.h"

@interface MAAdvTipView ()

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIImageView *titleView;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *upImgView;
@property (nonatomic, strong) UIImageView *downImgView;

@property (nonatomic, strong) UIButton *retryBtn;
@property (nonatomic, strong) UIButton *otherBtn;

@end

@implementation MAAdvTipView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [closeBtn addTarget:self action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:closeBtn];
        [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        [self addSubview:self.backView];
        [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
            make.width.ma_offset(294);
            make.height.ma_offset(227);
        }];
        
        _titleView = [UIImageView initWithName:@"Adv_TipTitle"];
        [self addSubview:_titleView];
        [_titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(_backView.mas_top);
            make.centerX.mas_equalTo(0);
        }];
        
    }
    return self;
}

- (void)retryEvent {
    MABlock(self.retryBlock);
    [self removeFromSuperview];
}

- (void)otherEvent {
    MABlock(self.otherBlock);
    [self removeFromSuperview];
}

- (void)setBecomeEnd:(BOOL)becomeEnd {
    _becomeEnd = becomeEnd;
    if (becomeEnd) {
        _titleView.image = [UIImage imageNamed:@"Adv_TipEnd"];
        
        _upImgView.hidden = _downImgView.hidden = YES;
        _titleLabel.font = MAFontScaleRegular(16);
        [_titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.ma_offset(43);
            make.right.ma_offset(-43);
            make.centerY.ma_offset(-5);
        }];
        
        [_backView addSubview:self.retryBtn];
        [_retryBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.ma_offset(30);
            make.bottom.ma_offset(-24);
            make.width.ma_offset(106);
            make.height.ma_offset(40);
        }];
        
        [_backView addSubview:self.otherBtn];
        [_otherBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.ma_offset(-30);
            make.bottom.ma_offset(-24);
            make.width.ma_offset(106);
            make.height.ma_offset(40);
        }];
    }
    
}

- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleLabel.text = title;
}

- (UIView *)backView {
    if (_backView) return _backView;
    _backView = [UIView new];
    _backView.backgroundColor = [UIColor whiteColor];
    _backView.layer.cornerRadius = MARatioWidth(20);
    _backView.layer.borderWidth = 1;
    _backView.layer.borderColor = UIColor.whiteColor.CGColor;
    _backView.clipsToBounds = YES;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MARatioWidth(288), MARatioWidth(98))];
    view.backgroundColor = [UIColor colorWithGradientStyle:UIGradientStyleTopToBottom withFrame:CGRectMake(0, 0, MARatioWidth(288), MARatioWidth(98)) andColors:@[MAColor(@"#FF0071"), UIColor.whiteColor]];
    [_backView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.ma_offset(98);
    }];
    
    [_backView addSubview:self.titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.ma_offset(54);
        make.right.ma_offset(-54);
        make.centerY.ma_offset(8);
    }];
    
    _upImgView = [UIImageView initWithName:@"Adv_UpDot"];
    [_backView addSubview:_upImgView];
    [_upImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.titleLabel.mas_top).ma_offset(-2);
        make.left.ma_offset(47);
    }];
  
    _downImgView = [UIImageView initWithName:@"Adv_DownDot"];
    [_backView addSubview:_downImgView];
    [_downImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).ma_offset(2);
        make.right.ma_offset(-47);
    }];
    
    return _backView;
}

- (UILabel *)titleLabel {
    if (_titleLabel) return _titleLabel;
    _titleLabel = [UILabel new];
    _titleLabel.font = MAFontScaleMedium(18);
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.adjustsFontSizeToFitWidth = YES;
    _titleLabel.numberOfLines = 5;
    return _titleLabel;
    
}

- (UIButton *)retryBtn {
    if (_retryBtn) return _retryBtn;
    _retryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _retryBtn.titleLabel.font = MAFontScaleMedium(16);
    _retryBtn.layer.borderColor = MAColor(@"#FF0071").CGColor;
    _retryBtn.layer.borderWidth = 1;
    _retryBtn.layer.cornerRadius = MARatioWidth(20);
    _retryBtn.layer.masksToBounds = YES;
    _retryBtn.backgroundColor = UIColor.clearColor;
    [_retryBtn setTitleColor:MAColor(@"#FF0071") forState:UIControlStateNormal];
    [_retryBtn setTitle:@"再来一局" forState:UIControlStateNormal];
    [_retryBtn addTarget:self action:@selector(retryEvent) forControlEvents:UIControlEventTouchUpInside];
    return _retryBtn;
}

- (UIButton *)otherBtn {
    if (_otherBtn) return _otherBtn;
    _otherBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _otherBtn.titleLabel.font = MAFontScaleMedium(16);
    _otherBtn.layer.cornerRadius = MARatioWidth(20);
    _otherBtn.layer.masksToBounds = YES;
    _otherBtn.backgroundColor = MAColor(@"#FF0071");
    [_otherBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [_otherBtn setTitle:@"玩玩其他" forState:UIControlStateNormal];
    [_otherBtn addTarget:self action:@selector(otherEvent) forControlEvents:UIControlEventTouchUpInside];
    return _otherBtn;
}
@end
